package com.example.skillcinema.ui.screens.main.viewmodels

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.skillcinema.db.displaymodels.DialogCollection
import com.example.skillcinema.db.displaymodels.FilmDisplayModel
import com.example.skillcinema.db.enums.FilmCollections
import com.example.skillcinema.db.enums.FilmTypes
import com.example.skillcinema.db.localdb.tables.CollectionEntity
import com.example.skillcinema.db.localdb.tables.FilmCollectionCrossRef
import com.example.skillcinema.db.localdb.tables.FilmEntity
import com.example.skillcinema.db.models.Film
import com.example.skillcinema.db.repositories.FilmRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class DialogCollectionViewModel @Inject constructor(val repository: FilmRepository) : ViewModel() {
    private val _collectionsStateFlow: MutableStateFlow<List<DialogCollection>> =
        MutableStateFlow(listOf())
    val collectionsFlow = _collectionsStateFlow.asStateFlow()
    private val _filmDataStateFlow: MutableStateFlow<FilmDisplayModel> = MutableStateFlow(
        FilmDisplayModel(0, "", 0f, "", "", FilmTypes.FILM, false)
    )
    val filmDataFlow = _filmDataStateFlow.asStateFlow()
    var filmId: Int = 0
    fun loadData() {
        viewModelScope.launch {
            _filmDataStateFlow.value = repository.getFilmById(filmId)
            val collections: List<CollectionEntity> = repository.getCollections()
            _collectionsStateFlow.value = collections.map { collection ->
                val filmCollections: List<FilmEntity> = repository.getCollectionFilms(collection)
                DialogCollection(
                    filmId,
                    collection.collectionId,
                    collection.type,
                    collection.name,
                    filmCollections.find { film -> film.filmId == filmId } != null,
                    filmCollections.count(),
                    collection.isSystem
                )
            }
        }
    }

    fun toggleCollection(collection: DialogCollection) {
        viewModelScope.launch {
            val filmData: FilmDisplayModel = filmDataFlow.value
            if (!collection.isInCollection) {
                repository.addFilmToCollection(
                    FilmEntity(
                        filmId,
                        filmData.imageUrl,
                        filmData.name,
                        filmData.rate,
                        filmData.genre,
                        filmData.filmTypes == FilmTypes.TV_SERIES
                    ), collection.collectionId
                )
            } else {
                repository.removeFilmFromCollection(filmId, collection.collectionId)
            }
            loadData()
        }
    }

    fun createCollection(name: String) {
        viewModelScope.launch {
            repository.createCollection(
                CollectionEntity(
                    0, name, CollectionEntity.CollectionType.CUSTOM, false
                )
            )
            loadData()
        }
    }
}